<?php

class Login extends CI_Controller {

	var $page = "login";

	public function index(){
		//echo "CONNECTED::::::::::::::::::::::";
		if(isset($_REQUEST['username'])&&isset($_REQUEST['password'])){


			$username  = $_REQUEST['username'];
			$password  = $_REQUEST['password'];
			$checkUser = $this->db->query("select * from users where  username = '$username' and password = '$password' and usertype='C' limit 1");



			if($checkUser->row() != null){
				$this->session->set_userdata('client_id', $checkUser->row()->id);
				$this->session->set_userdata('client_fullname', $checkUser->row()->firstname." ".$checkUser->row()->middlename." ".$checkUser->row()->lastname);
				$this->session->set_userdata('client_email', $checkUser->row()->email);
				$this->session->set_userdata('client_type', $checkUser->row()->usertype);
				$this->session->set_userdata('client_referencenumber', $checkUser->row()->referencenumber);
				$this->page = "home";
				$newsListLimit = " limit 5";
				$data['newsList'] = $this->db->query("SELECT * FROM pages WHERE parentid=(SELECT ID FROM PAGES WHERE NAME = 'news' LIMIT 1) $newsListLimit");
					
				$data =  $this->loadHomeContents($data);
			}


		}
		$data['selectedPagesInMenu'] = $this->db->query("select * from pages where menu_id != 0 order by menu_id");
			
		$query = $this->db->query("select * from users");
		$data['usertype'] = 'Client';
		$this->main($data,$query);
	}


	public function main($data,$query){

		$data['mastheadImage'] = 'masthead.jpg';
		$data['page'] = $this->page;
			
		$data['totalRecords'] = $query->num_rows();
		$data['query'] = $query;
			
		$this->loadClientPage($this->page,$data);
	}


	public function logout(){

		$this->session->unset_userdata('client_id');
		$this->session->unset_userdata('client_fullname');
		$this->session->unset_userdata('client_email');
		$this->session->unset_userdata('client_type');
		$this->session->unset_userdata('client_referencenumber');

		$data ='';
		$this->loadClientPage($this->page,$data);
	}


	function loadHomeContents($data){
		$newsListLimit = "limit 1";
		$data['aboutus'] = $this->db->query("SELECT * FROM pages WHERE NAME = 'aboutus' LIMIT 1 ")->row();
		$data['services'] = $this->db->query("SELECT * FROM pages WHERE NAME = 'services' LIMIT 1 ")->row();
		$data['contactus'] = $this->db->query("SELECT * FROM pages WHERE NAME = 'contactus' LIMIT 1 ")->row();
		return $data;

	}
}
